﻿@using SmartCode
@using SmartCode.Db;
@using SmartCode.Generator.Extensions
@model BuildContext
@{
    Layout = "_CSharpLayout.cshtml";
    var project = Model.Project;
    var buildTask = Model.Build;
    var table = Model.GetCurrentTable();
    var pkCol = table.PKColumn;
    if (pkCol == null)
    {
        throw new SmartCodeException($"Repository Template can not find PKColumn,Table:{table.Name}.");
    }
    var primaryKeyType = pkCol.LanguageType ?? "int";
    var autoIncrement = table.AutoIncrement;
    var dbSource = Model.GetDataSource<DbSource>();

    var pkNameEqId = pkCol.ConvertedName == "Id";
    var genericParameterKey = $"{table.ConvertedName},{primaryKeyType}";
}

using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using SmartSql.DyRepository;
using SmartSql.DyRepository.Annotations;
using @(project.Module).Entity;

namespace @(project.Module).@buildTask.Module
{
@table.GetCSharpSummary()
public interface I@(table.ConvertedName)Repository : IRepository<@genericParameterKey>
    ,IRepositoryAsync<@genericParameterKey>
        {
        @if (dbSource.DbRepository.DbProvider == DbProvider.PostgreSql && autoIncrement)
        {
            <text>[Statement(Execute = ExecuteBehavior.QuerySingle)]</text>
            @Html.NewLine()
            @Html.PadLeft(8) <text>new @primaryKeyType Insert(@table.ConvertedName entity);</text>
            @Html.NewLine()

            <text>[Statement(Execute = ExecuteBehavior.QuerySingle)]</text>
            @Html.NewLine()
            @Html.PadLeft(8) <text>new Task<@primaryKeyType> InsertAsync(@table.ConvertedName entity);</text>
            @Html.NewLine()
        }
        else if (autoIncrement && primaryKeyType != "int")
        {
            @Html.PadLeft(8) <text>new @primaryKeyType Insert(@table.ConvertedName entity);</text>
            @Html.NewLine()

            @Html.PadLeft(8) <text>new Task<@primaryKeyType> InsertAsync(@table.ConvertedName entity);</text>
            @Html.NewLine()
        }

        @if (!pkNameEqId)
        {
            <text>[Statement(Id = "GetEntity")]</text>
            @Html.NewLine()
            @Html.PadLeft(8) <text>new @table.ConvertedName GetById([Param("@pkCol.ConvertedName")]@primaryKeyType id);</text>
            @Html.NewLine()

            <text>[Statement(Id = "GetEntity")]</text>
            @Html.NewLine()
            @Html.PadLeft(8) <text>new Task<@table.ConvertedName> GetByIdAsync([Param("@pkCol.ConvertedName")]@primaryKeyType id);</text>
            @Html.NewLine()

            <text>[Statement(Id = "Delete")]</text>
            @Html.NewLine()
            @Html.PadLeft(8) <text>new int DeleteById([Param("@pkCol.ConvertedName")]@primaryKeyType id);</text>
            @Html.NewLine()

            <text>[Statement(Id = "Delete")]</text>
            @Html.NewLine()
            @Html.PadLeft(8) <text>new Task<@("int")> DeleteByIdAsync([Param("@pkCol.ConvertedName")]@primaryKeyType id);</text>
            @Html.NewLine()

        }
        }
        }
